package com.example.back.service.impl;

import com.example.back.constants.EscortReserveStatus;
import com.example.back.dto.resp.EscortReserveList;
import com.example.back.dto.resp.EscortReserveVo;
import com.example.back.mapper.EscortReserveMapper;
import com.example.back.service.EscortReserveService;
import com.example.common.support.ApiException;
import com.example.common.utils.CheckUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName EscortReserveServiceIpml
 * @Author wengjt
 * @Date 2021/5/7 10:12
 * @Description
 **/
@Service
public class EscortReserveServiceImpl implements EscortReserveService {
    @Resource
    private EscortReserveMapper escortReserveMapper;

    /**
     * 多条件分页查询陪护订单
     *
     * @Author wengjt
     * @Date 11:26 2021/5/7
     */
    @Override
    public EscortReserveList getEscortReserveList(String escortProductName, Integer escortTypeId, int[] rStatus, Integer pageNum, Integer pageSize) {
        EscortReserveList result = new EscortReserveList();

        PageInfo<EscortReserveVo> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            escortReserveMapper.multipleSearch(escortProductName, escortTypeId, rStatus);

        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 根据id查询陪护订单
     *
     * @Author wengjt
     * @Date 11:26 2021/5/7
     */
    @Override
    public EscortReserveVo getEscortReserveInfo(String escortReserveId) {
        return escortReserveMapper.findById(escortReserveId);
    }

    /**
     * 删除订单 逻辑删除
     *
     * @Author wengjt
     * @Date 11:27 2021/5/7
     */
    @Override
    public Object removeEscortReserve(String escortReserveId) {
        EscortReserveVo EscortReserveVo = escortReserveMapper.findById(escortReserveId);
        if (EscortReserveVo.getRStatus().equals(EscortReserveStatus.UN_PROCESS)) {
            throw new ApiException(400, "不允许删除未处理的订单");
        }
        escortReserveMapper.removeById(escortReserveId);
        return null;
    }

    /**
     * 处理订单 rStatus 1，代表结束 2，代表拒绝
     *
     * @Author wengjt
     * @Date 11:47 2021/5/7
     */
    @Override
    public Object processEscortReserve(String escortReserveId, Integer rStatus, String caregiverName, String caregiverPhone, String remark, String refuse) {
        CheckUtils.checkNull(rStatus, new ApiException(1, "对象不能为空"));
        if (rStatus.equals(EscortReserveStatus.AGREE)) {
            CheckUtils.checkNull(escortReserveId, new ApiException(1, "对象不能为空"));
            CheckUtils.checkNull(caregiverPhone, new ApiException(1, "对象不能为空"));
            CheckUtils.checkNull(remark, new ApiException(1, "对象不能为空"));
            escortReserveMapper.agree(escortReserveId, rStatus, caregiverName, caregiverPhone, remark);
        } else if (rStatus.equals(EscortReserveStatus.DISAGREE)) {
            CheckUtils.checkNull(remark, new ApiException(1, "对象不能为空"));
            CheckUtils.checkNull(refuse, new ApiException(1, "对象不能为空"));
            escortReserveMapper.disagree(escortReserveId, refuse, remark);
        }
        return null;
    }
}
